package com.pxr.slt.client;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.pxr.tutorial.xmltest.R;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Login  extends Activity{
	SharedPreferences androidRef;
	
	public void onCreate(Bundle savedInstanceState){
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.login);
	    androidRef = getSharedPreferences("username", MODE_PRIVATE);
	    
	    Button button = (Button) findViewById(R.id.login);
        button.setOnClickListener(new OnClickListener(){ // Add OnClick listener for when the user clicks the button
		    @Override
		    public void onClick(View v) {
		    	EditText username =  (EditText) findViewById(R.id.username);
		    	EditText password =  (EditText) findViewById(R.id.password);
		    	
		    	/*check login
		    	 * 
		    	 * JSONObject json = JSONfunctions.getJSONfromURL(Main.SERVER_ADDRESS + "message/json");
		    	try {
					JSONArray  loggedIn = json.getJSONArray("loggedIn");
				} catch (JSONException e) {
					Toast.makeText(Login.this, e.toString(), Toast.LENGTH_SHORT).show();
				}*/
		    	if(username.getText().toString().length() == 0){
		    		Toast.makeText(Login.this, "Geen gebruikersnaam ingevuld", Toast.LENGTH_SHORT).show();
		    	} else if(password.getText().toString().length() == 0){
		    		Toast.makeText(Login.this, "Geen wachtwoord ingevuld", Toast.LENGTH_SHORT).show();
		    	} else {

					Editor editor = androidRef.edit();    
					
		    		HttpClient httpClient = new DefaultHttpClient();
		    		HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 2000);
		    		HttpConnectionParams.setSoTimeout(httpClient.getParams(), 2000);
		    		HttpPost httpPost = new HttpPost(Main.SERVER_ADDRESS + "login/checklogin");  
		    		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
		    		nameValuePairs.add(new BasicNameValuePair("submit", "1"));  
		    		nameValuePairs.add(new BasicNameValuePair("username", username.getText().toString()));
		    		nameValuePairs.add(new BasicNameValuePair("password", password.getText().toString())); 		
		    		
		    		boolean loggedIn = false;
		    		try {
						httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
						HttpResponse response = httpClient.execute(httpPost);
						if(EntityUtils.toString(response.getEntity()).trim().equals("true")){
					    	loggedIn = true;
						}
						
					} catch (Exception e) {
						Toast.makeText(Login.this, e.toString(), Toast.LENGTH_SHORT).show();
					} 
					if(loggedIn){
						editor.putString("username",username.getText().toString());
						editor.commit();
						
			        	Intent intent = new Intent(Login.this, Main.class);
			            startActivity(intent);
					} else {
						Toast.makeText(Login.this, "Uw gebruikersnaam en/of wachtwoord is niet correct", Toast.LENGTH_SHORT).show();
					}		    		
		    	}
		     }
		});  	    
	}
}
